package cn.sciento.farm.devicemanager.gateway.model.downlink;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wumu
 * @version V1.0
 * @date 9/21/18
 * @time 4:46 PM
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SimpleBlockControlData extends BaseBlockControlData {


    @Override
    public MqttContent generate() {
        String topic = String.format(MqttConstant.SEND_SWITCH_TOPIC, concentrator);
        Map<String, Object> mqttSwitchData = new HashMap<>();
        mqttSwitchData.put("id", Integer.parseInt(node));
        mqttSwitchData.put("sid", Integer.parseInt(circuit));
        mqttSwitchData.put("val", en > 0 ? 1 : 0);
        mqttSwitchData.put("time", duration);
        String payload = JSON.toJSONString(mqttSwitchData);
        return new MqttContent(topic, payload);
    }

    public static ControlMqttDataBuilder builder() {
        return new ControlMqttDataBuilder();
    }


    public static final class ControlMqttDataBuilder {
        String concentrator;
        String node;
        String circuit;
        // 延迟
        int duration;
        // 开关
        int en;

        private ControlMqttDataBuilder() {
        }


        public ControlMqttDataBuilder concentrator(String concentrator) {
            this.concentrator = concentrator;
            return this;
        }

        public ControlMqttDataBuilder node(String node) {
            this.node = node;
            return this;
        }

        public ControlMqttDataBuilder circuit(String circuit) {
            this.circuit = circuit;
            return this;
        }

        public ControlMqttDataBuilder duration(int duration) {
            this.duration = duration;
            return this;
        }

        public ControlMqttDataBuilder en(int en) {
            this.en = en;
            return this;
        }


        public SimpleBlockControlData build() {
            SimpleBlockControlData simpleBlockControlData = new SimpleBlockControlData();
            simpleBlockControlData.circuit = this.circuit;
            simpleBlockControlData.node = this.node;
            simpleBlockControlData.duration = this.duration;
            simpleBlockControlData.en = this.en;
            simpleBlockControlData.concentrator = this.concentrator;
            return simpleBlockControlData;
        }
    }
}
